import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const channel = MethodChannel("timesdk_plugin");

  @override
  void initState() {
    super.initState();
    channel.setMethodCallHandler((call) async {
      print("接收到call: ${call.method} arguments: ${call.arguments}");
    });
  }

  void errorHandler(e) {
    print("=====TimesdkException=====");
    print(e.toString());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            tButton('collectMessage', (){
              collectMessage();
            }),
          ],
        ),
      )// This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  void collectMessage() {
    var data = {
      "action":"timeSDK",
      "id":"0.6656999111432877",
      "data": {
        "orderNo":"0a729c8f-f434-4232-96c4-ba38fc963c5b",
        "userId":"1423538032933470208",
        "isSubmit":false,"appList":false,
        "sms":false,"exif":false,"device":false,
        "contact":false,"location":false},"callback":"webViewToTime"
    };
    channel.invokeMethod('collectMessage', json.encode(data));

  }

  Widget tButton(String text, Function? onTap){
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
      margin: const EdgeInsets.only(bottom: 10),
      decoration: BoxDecoration(
        border: Border.all(
          width: 1,
          color: Colors.black
        )
      ),
      child: GestureDetector(
        onTap: () {
          onTap?.call();
        },
        child: Text(text),
      ),
    );
  }
}
